<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="50%" destroy-on-close @closed="$emit('closed')" :close-on-click-modal="false">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="供应商名称" prop="apply_name">
                        <el-input v-model="form.apply_name" placeholder="请输入供应商名称" clearable autocomplete="off"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="简称" prop="referred_as">
                        <el-input v-model="form.referred_as" placeholder="请输入供应商简称" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="供应商类型" prop="apply_cate">
                        <el-select v-model="form.apply_cate" placeholder="请选择供应商类型" style="width:100%" @change="change">
                            <!-- <el-option label="工厂供应商" :value="1"></el-option>
                            <el-option label="工序供应商" :value="2"></el-option> -->
                            <el-option label="毛料供应商" :value="3"></el-option>
                            <el-option label="辅料供应商" :value="4"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="供应商地址" prop="address">
                        <el-input v-model="form.address" placeholder="请输入地址" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="联系人" prop="linkman">
                        <el-input v-model="form.linkman" placeholder="请输入联系人" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="手机号" prop="mobile">
                        <el-input v-model="form.mobile" placeholder="请输入手机号" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="初始密码" prop="password">
                        <el-input v-model="form.password" placeholder="请输入密码" clearable></el-input>
                         <!-- show-password type="password" -->
                    </el-form-item>
                </el-col>
            </el-row>
			<el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="税号" prop="tax_identification_no">
                        <el-input v-model="form.tax_identification_no" placeholder="请输入税号" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="结算方式" prop="settlement_way">
                        <el-select v-model="form.settlement_way" filterable placeholder="结算方式" style="width:100%">
                            <el-option v-for="item in settlementList" :key="item.id" :label="item.title" :value="item.id" />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="简介" prop="desc">
                        <el-input v-model="form.desc" clearable placeholder="请输入简介"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="状态" prop="system_disable">
                        <el-switch v-model="form.system_disable" :active-value="0" :inactive-value="1"></el-switch>
                    </el-form-item>
                </el-col>
            </el-row>	
            <el-row v-if="type==1">
                <el-col :span="12">
                    <el-form-item label="擅长类目" prop="goods_at_goods_cate">
                        <el-select v-model="form.goods_at_goods_cate" filterable placeholder="请选择" style="width:100%" multiple>
                            <el-option v-for="item in cates" :key="item.id" :label="item.title" :value="item.id" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="擅长针种" prop="goods_at_needle_seed">
                        <el-select v-model="form.goods_at_needle_seed" filterable placeholder="请选择" style="width:100%" multiple>
                            <el-option v-for="item in needle" :key="item.id" :label="item.title" :value="item.id" />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <!-- <el-col :span="12" v-if="type==1">
                <el-form-item label="擅长工艺" prop="goods_at_technology">
                    <el-select v-model="form.goods_at_technology" filterable placeholder="请选择" style="width:100%" multiple>
                        <el-option v-for="item in technology" :key="item.id" :label="item.title" :value="item.id" />
                    </el-select>
                </el-form-item>
            </el-col> -->
            <el-col :span="12" v-if="type== 1 || type== 2">
                <el-form-item label="工序" prop="goods_at">
                    <el-select v-model="form.goods_at" filterable placeholder="请选择" style="width:100%" multiple>
                        <el-option v-for="item in Gongxu" :key="item.id" :label="item.title" :value="item.id" />
                    </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="12" v-if="type==3">
                <el-form-item label="擅长毛种" prop="goods_at_material_cate">
                    <el-select v-model="form.goods_at_material_cate" filterable placeholder="请选择" style="width:100%" multiple>
                        <el-option v-for="item in arrayType" :key="item.id" :label="item.material_name" :value="item.id" />
                    </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="12" v-if="type==4">
                <el-form-item label="类别" prop="goods_at_material_cate">
                    <el-select v-model="form.goods_at_material_cate" filterable placeholder="请选择" style="width:100%" multiple>
                        <el-option v-for="item in arrayType" :key="item.id" :label="item.material_name" :value="item.id" />
                    </el-select>
                </el-form-item>
            </el-col>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
import { ElMessageBox  } from 'element-plus'
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增供应商',
				edit: '编辑供应商',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				apply_id: "",
				apply_name: "",
				referred_as: "",
				linkman: "",
				mobile: "",
				tax_identification_no: "",
				settlement_way: "",
				system_disable: 0,
				address: "",
				desc: "",
				apply_cate: "",
                password:"",
                goods_at:"",
                goods_at_material_cate:"",
                goods_at_goods_cate:"",
                goods_at_needle_seed:"",
                goods_at_technology:"",
                is_confirm:0
			},
			//验证规则
			rules: {
				apply_name: [
					{ required: true, message: '请输入供应商名称' }
				],
				referred_as: [
					{ required: true, message: '请输入供应商简称' }
				],
				linkman: [
					{ required: true, message: '请输入联系人' }
				],
				mobile: [
					{ required: true, message: '请输入手机号' }
				],
				type: [
					{ required: true, message: '请选择供应商类型' }
				],
			},
            type:"",
			settlementList: [],
			cate_disabled: false,
            arrayType:[],
            needle:[],
            technology:[],
            cates:[],
            Gongxu:[]
		}
	},
	mounted() {
		this.getAllSettlementMethod()
        this.getNeedle()
        // this.getTechnology()
        this.getType()
        this.getGong()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.apply_id) {
						res = await this.$API.basic.apply.edit.post(this.form);
					} else {
						res = await this.$API.basic.apply.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else if(res.code==2) {
                        this.form.is_confirm = 1
                        ElMessageBox.confirm(
                            res.message,
                            "提示",
                            {
                                confirmButtonText: '确定',
                                cancelButtonText: '取消',
                                type: 'warning',
                            }
                        ).then(() => {
                            this.sub()
                        }).catch(() => {
                            this.form.is_confirm = 0
                        })
                    }else {
                        this.$alert(res.message, "提示", { type: 'error' })
                    }
				} else {
					return false;
				}
			})
		},
        async sub(){
            this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.apply_id) {
						res = await this.$API.basic.apply.edit.post(this.form);
					} else {
						res = await this.$API.basic.apply.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
                        this.form.is_confirm = 0
                        this.$alert(res.message, "提示", { type: 'error' })
                    }
				} else {
					return false;
				}
			})
        },
		//表单注入数据
		async setData(data) {
            console.log('data :>> ', data);
			this.form.apply_id = data.apply_id
			if (data.apply_id) {
				this.cate_disabled = true
			}
			this.form.apply_name = data.apply_name
			this.form.referred_as = data.referred_as
			this.form.linkman = data.linkman
			this.form.mobile = data.mobile
			this.form.tax_identification_no = data.tax_identification_no
			this.form.settlement_way = Number(data.settlement_way)
			this.form.system_disable = data.system_disable
			this.form.address = data.address
			this.form.remark = data.remark
            this.form.apply_cate = data.apply_cate
            this.form.goods_at = data.goods_at
            this.form.password = data.password
            this.form.goods_at_goods_cate = data.goods_at_goods_cate
            this.form.goods_at_needle_seed = data.goods_at_needle_seed
            this.form.goods_at_technology = data.goods_at_technology
            this.type = data.apply_cate
            if(data.apply_cate==3){
                var res = await this.$API.all.material.get({category_id:1});
                this.arrayType = res.data;
            }else if(data.apply_cate==4){
                var a = await this.$API.all.material.get({category_id:2});
                this.arrayType = a.data;
            }
            this.form.goods_at_material_cate = data.goods_at_material_cate
		},
        async change(e){
            this.goods_at=""
            this.goods_at_material_cate=""
            this.goods_at_goods_cate=""
            this.goods_at_needle_seed="",
            this.goods_at_technology="",
            this.type = e
            if(e==3){
                var res = await this.$API.all.material.get({category_id:1});
                this.arrayType = res.data;
            }else if(e==4){
                var data = await this.$API.all.material.get({category_id:2});
                this.arrayType = data.data;
            }
        },
        // 获取针种
        async getNeedle(){
            var res = await this.$API.all.needle.get();
            this.needle = res.data;
        },
        // 获取工艺
        // async getTechnology(){
        //     var res = await this.$API.all.technology.get();
        //     this.technology = res.data;
        // },
        // 获取类目
        async getType(){
            var res = await this.$API.all.type.get();
            this.cates = res.data;
        },
        async getAllSettlementMethod() {
            var res = await this.$API.common.getAllSettlementMethod.get();
				this.settlementList = res.data
		},
        // 获取工序
        async getGong() {
			var res = await this.$API.all.process.get({pid:0});
			this.Gongxu = res.data
		},
	}
}
</script>

<style></style>
